package com.leadingsoft.web.code.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.leadingsoft.common.constant.Constant;
import com.leadingsoft.core.dto.BaseQuery;
import com.leadingsoft.core.pojo.Code;
import com.leadingsoft.core.pojo.UserInfo;
import com.leadingsoft.web.code.service.CodeService;

@RestController
@RequestMapping(value = "/w/code/", produces = "text/plain;charset=UTF-8")
public class CodeController {
	@Autowired
	private CodeService CodeService;

	@RequestMapping("types")
	public String getCodeTypes() {
		return CodeService.getCodeTypes();
	}
	@RequestMapping("categorys")
	public String getCodeCategory() {
		return CodeService.getCodeCategorys();
	}
	@RequestMapping("codeList")
	public String codeList(HttpServletRequest request, BaseQuery baseQuery, String type, String category, int isCreate,
			int isUser) {
		Integer uId = null;
		if (isUser == 1) {
			UserInfo userInfo = (UserInfo) request.getSession().getAttribute(Constant.SESSION_ID);
			uId = userInfo.getuId();
		}
		return CodeService.getList(baseQuery, uId, type, category, isCreate);
	}
	@RequestMapping("saveCode")
	public String saveCode(HttpServletRequest request, Code code) {
		UserInfo userInfo = (UserInfo) request.getSession().getAttribute(Constant.SESSION_ID);
		Integer uId = userInfo.getuId();
		return CodeService.saveCode(uId, code);
	}
	@RequestMapping("viewCode")
	public String viewCode(Integer codeId) {
		return CodeService.viewCode(codeId);
	}
}
